package cc.xpcas.raft.context;

import cc.xpcas.raft.message.Message;
import cc.xpcas.raft.message.MessageContext;
import cc.xpcas.raft.network.Address;
import cc.xpcas.raft.network.Endpoint;
import cc.xpcas.raft.network.Packet;

/**
 * @author xp
 */
public class Peer {

    private final int serverId;

    private final Address address;

    private final Endpoint endpoint;

    public Peer(int serverId, Address address, Endpoint endpoint) {
        this.serverId = serverId;
        this.address = address;
        this.endpoint = endpoint;
    }

    public void send(Message message) {
        Packet packet = new Packet(null, address, message);
        endpoint.send(packet);
    }

    public MessageContext wrapReceivedMessage(Message message) {
        return new MessageContext(this::send, message);
    }
}
